﻿// <copyright file="RepeatedSwitchUTelegram.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author> 

namespace EnOcean.Radio.Sensor
{
  using System;

  /// <summary>
  /// U-message from a switch module (ORG = 0x05).
  /// </summary>
  [Serializable]
  public class RepeatedSwitchUTelegram : RepeatedSwitchTelegram
  {
    /// <summary>
    /// Variable for the Buttons property.
    /// </summary>
    private byte buttons;

    /// <summary>
    /// Gets or sets the number of simultaneously pressed buttons.
    /// </summary>
    public byte Buttons
    {
      get
      {
        return this.buttons;
      }

      set
      {
        if (value >= 0x0 && value <= 0x7)
        {
          this.buttons = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("Buttons", "must be a 3 bit value in the range 0x0 to 0x7 inclusive");
        }
      }
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      base.CreateMessage(message);

      // Data 3
      message[4] = (byte)((uint)this.Buttons << 5);

      if (this.IsEnergyBowPressed)
      {
        message[4] |= 0x10;
      }

      // Status
      message[12] = RepeatCounter;

      if (this.SwitchType == SwitchType.Type1)
      { // Type
        message[12] &= 0xDF;
      }
      else if (this.SwitchType == SwitchType.Type2)
      {
        message[12] |= 0x20;
      }

      message[12] &= 0x3F; // Reserved
    }
  }
}
